﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">

        alert(extractLast("aaa <b>sf<i>sd<u> this</i ></u></b>"));

        function extractLast(term) {
            //remove html tags
            var elementsInTerms = new Array();

            elementsInTerms[0] = ""; //begining element
            elementsInTerms[1] = term; //found word
            elementsInTerms[2] = ""; //ending element
            elementsInTerms[3] = 0; //location for first letter of the found word

            var initialText = term;
            var endingElement = "";
            var foundWord = "";
            var foundWordTemp = "";
            var beginningElement = "";

            var whiteSpaceRegex = /(\s|\n|\r|\t)/g / m;

            var i = initialText.length - 1;

            //see if the last character is a non-white space or new line char. if true, do not find any word
            if (!whiteSpaceRegex.test(initialText.charAt(i))) {

                while (i >= 0) {
                    if (initialText.charAt(i) == ">" || !whiteSpaceRegex.test(initialText.charAt(i))) {

                        while (initialText.charAt(i) != "<" && i >= 0) {
                            endingElement += initialText.charAt(i);
                            i--;
                        }
                        endingElement += initialText.charAt(i);
                        i--;

                        while (initialText.charAt(i) != ">" && (!whiteSpaceRegex.test(initialText.charAt(i))) && i >= 0) {
                            foundWordTemp += initialText.charAt(i);
                            i--;
                        }
                        //                        if (foundWordTemp.length > 0) {
                        //                            foundWord = foundWordTemp.reverse();
                        ////                            for (var x = foundWordTemp.length; x > 0; x--) {
                        ////                                foundWord += foundWordTemp.charAt(x - 1);
                        ////                            }
                        //                            //foundWordIndex = i;
                        //                            break;
                        //                        }

                    } else {
                        while (initialText.charAt(i) != ">" && (!whiteSpaceRegex.test(initialText.charAt(i))) && i >= 0) {
                            foundWordTemp += initialText.charAt(i);
                            i--;
                        }
                        //                        if (initialText.charAt(i) != ">" || !whiteSpaceRegex.test(initialText.charAt(i))) {
                        //                            endingElement += initialText.charAt(i);
                        //                            //i--;
                        //                        }
                        //                        if (foundWordTemp.length > 0) {
                        //                            foundWord = foundWordTemp.reverse();
                        ////                            for (var x = foundWordTemp.length; x > 0; x--) {
                        ////                                foundWord += foundWordTemp.charAt(x - 1);
                        ////                            }
                        //                            //foundWordIndex = i;
                        //                            break;
                        //                        }
                    }
                    //i--;
                }




            }else if (whiteSpaceRegex.test(initialText.charAt(i))) {
                while (i >= 0) {
                    beginningElement += initialText.charAt(i);
                    i--;
                }
            }
            
            if (foundWordTemp.length > 0) {
                foundWord = foundWordTemp.reverse();
            }

            this.foundWordIndex = i;
            foundWord = foundWord.split(" ").pop();

            //alert("searching:" + lastWord);
            return elementsInTerms;
        }

    </script>
</body>
</html>
